package com.lzw.kotdemo.bean
import com.lzw.kotdemo.model.Forecast as ModelForecast
import java.text.DateFormat
import java.util.*

/**KotlinDemo
 * Created by lzw on 2019/1/2. 14:51:51
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
 class ForecastDataMapper{
    fun convertFromDataModel(forecast: ForecastResult):ForecastList{
        return ForecastList(forecast.city.name,forecast.city.country,convertForecastListToDomain(forecast.list))
    }

    private fun convertForecastListToDomain(list:List<Forecast>):List<ModelForecast>{
        return list.map { convertForecastItemToDomain(it) }
    }

    private fun convertForecastItemToDomain(forecast: Forecast):ModelForecast{
        return ModelForecast(convertDate(forecast.dt),forecast.weather[0].description,forecast.temp.max.toInt(),
                forecast.temp.min.toInt(),generateIconUrl(forecast.weather.get(0).icon))
    }

    private fun generateIconUrl(iconCode: String): String="http://openweathermap.org/img/w/$iconCode.png"

    private fun convertDate(date:Long):String{
        val df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault())
        return df.format(date * 1000)
    }
}